package cn.xfdzx.android.apps.shop.adapter;

import android.content.Intent;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.bitmap.CenterCrop;
import com.bumptech.glide.load.resource.bitmap.RoundedCorners;
import com.bumptech.glide.request.RequestOptions;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.luck.picture.lib.tools.ToastUtils;

import java.util.ArrayList;
import java.util.List;

import cn.xfdzx.android.apps.shop.R;
import cn.xfdzx.android.apps.shop.activity.AfterServiceInfoActivity;
import cn.xfdzx.android.apps.shop.activity.ShopActivity;
import cn.xfdzx.android.apps.shop.bean.AfterSalesListRetailBean;
import cn.xfdzx.android.apps.shop.bean.OrderDetailInfoBeen;
import cn.xfdzx.android.apps.shop.util.ImageGlideUtil;

/**
 * Creator: zf.
 * Date: 2020/9/24.
 * Description:零售-售后列表适配器.
 */
public class AfterSalesListRetailAdapter extends BaseQuickAdapter<AfterSalesListRetailBean.DataBean.ApplyBean, BaseViewHolder>  {
    private OnItemOnClickListener onItemOnClickListener;
    private List<AfterSalesListRetailBean.DataBean.ApplyBean> cartInfo = new ArrayList<>();//列表
    private AfterSalesListRetail_InnergoodsAdapter adapter;
    public AfterSalesListRetailAdapter(@Nullable List<AfterSalesListRetailBean.DataBean.ApplyBean> data) {
        super(R.layout.item_order_aftersales, data);
    }

    @Override
    public void setData(int index, @NonNull AfterSalesListRetailBean.DataBean.ApplyBean data) {
        super.setData(index, data);
    }


    @Override
    protected void convert(final BaseViewHolder viewHolder, final AfterSalesListRetailBean.DataBean.ApplyBean date) {
        View viewGotoShop = viewHolder.getView(R.id.view_goto_shop);
        TextView tv_afterinfo = viewHolder.getView(R.id.tv_afterinfo);
        TextView tv_state = viewHolder.getView(R.id.tv_state);
        ImageView iv_shop = viewHolder.getView(R.id.iv_shop);
        RecyclerView rvOrderGoods = viewHolder.getView(R.id.rv_order_goods);
        RoundedCorners roundedCorners = new RoundedCorners(20);
        //通过RequestOptions扩展功能,override:采样率,因为ImageView就这么大,可以压缩图片,降低内存消耗
        RequestOptions options = RequestOptions.bitmapTransform(roundedCorners).override(200, 200);
        options.placeholder(R.drawable.icon_defult)//图片加载出来前，显示的图片
                .fallback(R.drawable.icon_defult) //url为空的时候,显示的图片
                .error(R.drawable.icon_defult);

        if (null != date.getStoreInfo().getStore_logo()) {
            Glide.with(mContext).load(date.getStoreInfo().getStore_logo()).apply(options.transform(new CenterCrop(), roundedCorners)).into(iv_shop);
        }

        switch (String.valueOf(date.getStatus())){
            case "1":
            case "2":
            case "5":
                tv_state.setText("退款处理中");
            break;
            case "3":
                tv_state.setText("退款被驳回");
                break;
            case "4":
                tv_state.setText("退款已取消");
                break;
            case "6":
                tv_state.setText("退款成功");
                break;
        }

        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(mContext);
        linearLayoutManager.setOrientation(LinearLayoutManager.HORIZONTAL);
        rvOrderGoods.setLayoutManager(linearLayoutManager);
        adapter = new AfterSalesListRetail_InnergoodsAdapter(date.getApplyCart());
        rvOrderGoods.setAdapter(adapter);

        viewHolder.setText(R.id.tv_shop_name,date.getStoreInfo().getOffline_shop_name()+"")
        .setText(R.id.tv_order_time,date.getCreated_at())
        .setText(R.id.tv_total_num,"共" + date.getBuy_num() + "件")
        .setText(R.id.tv_total_money,"¥" +date.getPay_price());
//
//        ImageGlideUtil.loadImage(mContext,imageView,date.getProductInfo().getImage());
        tv_afterinfo.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(mContext, AfterServiceInfoActivity.class);
                intent.putExtra("orderId", date.getOrder_id());
                intent.putExtra("applyId", date.getId()+"");
                intent.putExtra("type", date.getSales_model() + "");
                mContext.startActivity(intent);
            }
        });

       viewGotoShop.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(mContext, ShopActivity.class);
                intent.putExtra("shop_id",date.getStore_id()+"");
                mContext.startActivity(intent);
            }
        });
    }
    public void clearDatas(){
        mData.clear();
        notifyDataSetChanged();
    }
    public void refreshDatas(List<AfterSalesListRetailBean.DataBean.ApplyBean> list){
        this.cartInfo = list;
        super.replaceData(list);
    }

    public void setItemAddMoveListener(OnItemOnClickListener listener){
        this.onItemOnClickListener = listener;
    }

    public interface OnItemOnClickListener {
        void onItemAddMoveClick(int position, int count, AfterSalesListRetailBean.DataBean.ApplyBean date);
    }

}
